<?php
/**
 * Class to connect a mysql database
 * 
 * @copyright Lellys InformÃ¡ica
 * @author Ã�talo Lelis de Vietro
 * @link http://www.lellysinformatica.com
 * 
 * @version 1.0
 * 
 * @license GNU General Public License, version 3 (GPL-3.0)
 * @see http://www.opensource.org/licenses/gpl-3.0.html
 */
class db {
	
	/**
	 * Mysql username
	 * @var string 
	 */
	private $username;
	/**
	 * Mysql password
	 * @var string 
	 */
	private $password;
	/**
	 * Connection link
	 * @var resource 
	 */
	private $link;
	/**
	 * Database name
	 * @var string
	 */
	private $database;
	/**
	 * Server name
	 * @var string 
	 */
	private $server;
	public $tbl_fix = '';
	
	public function getUsername() {
		return $this->username;
	}
	
	public function setUsername($username) {
		$this->username = $username;
	}
	
	public function getPassword() {
		return $this->password;
	}
	
	public function setPassword($password) {
		$this->password = $password;
	}
	
	public function getLink() {
		return $this->link;
	}
	
	public function getDatabase() {
		return $this->database;
	}
	
	public function setDatabase($database) {
		$this->database = $database;
	}
	
	public function getServer() {
		return $this->server;
	}
	
	public function setServer($server) {
		$this->server = $server;
	}
	
	/**
	 * Initialize a new instance of easy_connection
	 * 
	 * @param string $username
	 * @param string $password
	 * @param string $database
	 * @param string $server 
     
    function __construct($username, $password, $database, $server) {
        $this->username = $username;
        $this->password = $password;
        $this->database = $database;
        $this->server = $server;
    }
	 */
	/**
	 * Connect to the database
	 */
	public function connect() {
		$this->link = mysql_connect ( $this->server, $this->username, $this->password ) or die ( mysql_error () );
		mysql_select_db ( $this->database, $this->link ) or die ( mysql_error () );
		$this->setNames ();
	}
	
	/**
	 * Close the connection
	 * @return resource 
	 */
	public function close() {
		return mysql_close ( $this->link );
	}
	
	/**
	 * Set all fields to the desired type of encoding
	 * @param string $encode 
	 */
	private function setNames($encode = 'UTF8') {
		mysql_query ( "SET NAMES '$encode'" );
	}
	
	/**
	 * Executes a sql instruction
	 * 
	 * @param string $sql
	 * @param resource $return_format
	 * @return type 
	 */
	public function executeQuery($sql, $return_format = 0) {
//		echo $sql;
		if ($this->link == NULL)
			$this->connect ();
		$query = mysql_query ( $sql, $this->link ) or die ( mysql_error () );
		switch ($return_format) {
			case 1 :
				$query = mysql_fetch_assoc ( $query );
				break;
			case 2 :
				$query = mysql_fetch_array ( $query );
				break;
			case 3 :
				$query = mysql_fetch_row ( $query );
				return $query [0];
			default :
				return $query;
		}
		return $query;
		$this->close ();
	}
	public function record_insert($table_name, $data)   {
        $queryString = "INSERT INTO " . $table_name . " (";
        $columns = array();
        $values = array();

        foreach ($data as $key => $value) {
            $columns[] = '`' . $key . '`';
            $values[] = "'" . $value . "'";
        }
        $queryString .= implode(',', $columns) . ") VALUES (" . implode(',', $values) . ") ";
        return $this->executeQuery($queryString);
    }
    function record_update($table_name, $data, $where = false)    {
        $queryString = "UPDATE ". $table_name . " SET ";
        $fields = array();

        foreach ($data as $key => $value) {
            $fields[] = " `$key`='" . $value . "' ";
        }
		if ($where) $where_and = " WHERE " . $where;
        $queryString .= implode(',', $fields) . $where_and;
        return $this->executeQuery($queryString,"update");
    }
    public function record_delete($table_name, $where='')    {
        if ($where)	$whereand = " WHERE ".$where;
        $queryString = "DELETE FROM " . $table_name . $whereand;
        $this->executeQuery($queryString);
    }

}

?>
